package com.example.administrator.demonprocessdemo;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;

public class RemoteService extends Service {
    private RemoteService.RemoteServiceBinder remoteBinder;
    private RemoteService.RemoteServiceConnection remoteConn;
    private int count;
    private Timer timer;

    public RemoteService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        return remoteBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (remoteBinder == null){
            // LocalServiceBinder 是静态的内部类  要判空 否则会泄漏
            remoteBinder = new RemoteService.RemoteServiceBinder();

        }

        remoteConn = new RemoteService.RemoteServiceConnection();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setContentTitle("我是曾令文");
        builder.setContentInfo("我是Info");
        builder.setWhen(System.currentTimeMillis());
        PendingIntent p = PendingIntent.getActivity(this,0,intent,0);
        builder.setContentIntent(p);
        Notification notification = builder.build();
        startForeground(startId,notification);

        startTimer();
        return Service.START_STICKY;

    }

    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.e(TAG, "run: Remote" + count );
                count++;
            }
        },100);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    class RemoteServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG, "onServiceConnected: " + name  );
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "onServiceDisconnected: " + name );

            startService(new Intent(RemoteService.this, LocalService.class));
            bindService(new Intent(RemoteService.this, LocalService.class), remoteConn, Context.BIND_IMPORTANT);
        }
    }

    class RemoteServiceBinder extends IMyAidlInterface.Stub{

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    }
}
